package com.sunday.volunteer.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.event.RegistSuccess;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.ui.MainActivity;
import com.sunday.volunteer.utils.BaseUtil;
import com.sunday.volunteer.utils.StatusBarUtil;

import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginActivity extends BaseActivity {

    @Bind(R.id.checkbox)
    TextView checkBok;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.password)
    TextView password;

    private final int SEVEN_DAYS= 7*24*60*60*1000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        StatusBarUtil.setTransparentForImageView(this , null);

        long oldLoginTime = SharePerferenceUtils.getIns(mContext).getLong("login_time",0);

        if (SharePerferenceUtils.getIns(mContext).getBoolean("is_loginSelf",false)){
            if ((Calendar.getInstance().getTimeInMillis() - oldLoginTime)<SEVEN_DAYS){
                if (SharePerferenceUtils.getIns(mContext).getOAuth()!=null){
                    Intent intent = new Intent(mContext, MainActivity.class);
                    startActivity(intent);
                }
            }
        }

    }

    private boolean check;
    @OnClick(R.id.checkbox)
    void check(){
        check = !check;
        checkBok.setSelected(check);
    }

    public void ForgetPassword(View view){
        BaseUtil.openSendCodeActivity(mContext , 2);
    }


    public void Register(View view){
        BaseUtil.openActivity(mContext , RegisterActivity.class);
    }


    public void Login(View view){
        if(!StringUtils.isMobileNO(userName.getText().toString())){
            ToastUtils.showToast(mContext , "手机号码不合法");
            return;
        }
        if(TextUtils.isEmpty(password.getText().toString())){
            ToastUtils.showToast(mContext , "请输入密码");
            return;
        }
        showLoadingDialog(0);
        Call<ResultDO<Member>> call = HttpClient.getHttpAdapter().login(userName.getText().toString() ,password.getText().toString() ,1);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    Member member = response.body().getResult();
                    if (checkBok.isSelected()){
                        SharePerferenceUtils.getIns(mContext).putBoolean("is_loginSelf", true);
                        SharePerferenceUtils.getIns(mContext).putLong("login_time", Calendar.getInstance().getTimeInMillis());
                    }
                    SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                    BaseApp.getInstance().setMemberId(member.getId());
                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);

                    BaseUtil.openActivity(mContext , MainActivity.class);
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }



}
